package com.chen.pfm.utils;

import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.temporal.TemporalAdjusters;
import java.util.*;


public class DateUtils {

    private static String pattern = "yyyy-MM-dd HH:mm:ss";
    private static String patternyMd = "yyyy-MM-dd";
    private static String patternyMdBWithChinese = "yyyy年MM月dd日";
    private static SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
    private static String CurrentClassName = "DateUtils";
    static final HiLogLabel hiLogLabel = new HiLogLabel(HiLog.LOG_APP, 0x2022001, CurrentClassName);
    //时间 出生日期计算年龄

    /**
     *
     * @param birth
     * @return
     */
    /**
     * 根据yyyy-MM-dd计算年龄
     *
     * @param
     * @return
     */
    public static String getAgeFromBirthday(String birthday) {

        //截取人出生日期中的年、月、日
        Integer personYear = getYear(birthday);
        Integer personMonth = getMonth(birthday);
        Integer personDay = getDay(birthday);

        Calendar cal = Calendar.getInstance();
        // 得到当前时间的年、月、日
        Integer yearNow = cal.get(Calendar.YEAR);
        Integer monthNow = cal.get(Calendar.MONTH) + 1;
        Integer dayNow = cal.get(Calendar.DATE);

        // 用当前年月日减去生日年月日
        Integer yearMinus = yearNow - personYear;
        Integer monthMinus = monthNow - personMonth;
        Integer dayMinus = dayNow - personDay;

        Integer age = yearMinus; //先大致赋值

        if (yearMinus == 0) { //出生年份为当前年份
            age = 0;
        } else { //出生年份大于当前年份
            if (monthMinus < 0) {//出生月份小于当前月份时，还没满周岁
                age = age - 1;
            }
            if (monthMinus == 0) {//当前月份为出生月份时，判断日期
                if (dayMinus < 0) {//出生日期小于当前月份时，没满周岁
                    age = age - 1;
                }
            }
        }
        return String.valueOf(age);
    }


    public static String[] getSelectStatisticalYearList(int n) {
        int size = n * 2 + 1;
        String[] yearList = new String[size];
        Calendar calendar = Calendar.getInstance();
        int currentYEAR = calendar.get(Calendar.YEAR);
        DecimalFormat Dformat = new DecimalFormat("0000");
        int count = 0;
        for (int i = n; i >= 0; i--) {
            yearList[count++] = Dformat.format(currentYEAR - i);
        }
        for (int i = 0; i < n; i++) {
            yearList[count++] = Dformat.format(currentYEAR + i);
        }

        return yearList;
    }

    //得到12个月份数
    public static String[] getSelectStatisticalMonthList() {
        String[] monthList = new String[12];
        DecimalFormat Dformat = new DecimalFormat("00");
        for (int i = 0; i < 12; i++) {
            monthList[i] = Dformat.format(i + 1);
        }
        return monthList;
    }

    //得到一个年的月份拼接列表
    public static String[] getX_axisByYear(String year) {
        String[] monthList = getSelectStatisticalMonthList();

        String[] X_axis = new String[12];
        for (int i = 0; i < 12; i++) {
            X_axis[i] = year + "-" + monthList[i];
        }
        return X_axis;
    }

    //获取某个月的天数
    public static int getDayNumbersByYearMonth(String yearMonth) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.setTime(sdf.parse(yearMonth));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    //得到一个月的日期拼接列表
    public static String[] getX_axisByMonth(String yearMonth) {
        int monthDays = getDayNumbersByYearMonth(yearMonth);
        String[] X_axis = new String[monthDays];
        DecimalFormat Dformat = new DecimalFormat("00");
        for (int i = 0; i < monthDays; i++) {
            X_axis[i] = yearMonth + "-" + Dformat.format(i + 1);
        }
        return X_axis;
    }

    //获取当前年
    public static String getStrCurrentYear() {
        Calendar calendar = Calendar.getInstance();
        int CurrentYEAR = calendar.get(Calendar.YEAR);
        DecimalFormat Dformat = new DecimalFormat("0000");
        return Dformat.format(CurrentYEAR);
    }

    //获取当前年
    public static int getCurrentYear() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));    //获取东八区时间
        return calendar.get(Calendar.YEAR);    //获取年
    }

    //获取当前月
    public static int getCurrentMonth() {
        Calendar calendar = Calendar.getInstance();
        //获取月份，0表示1月份
        return calendar.get(Calendar.MONTH) + 1;
    }

    //获取当前月
    public static String getStrCurrentMonth() {
        int CurrentMonth = getCurrentMonth();
        DecimalFormat Dformat = new DecimalFormat("00");
        return Dformat.format(CurrentMonth);
    }

    //获取当前号  注意这里是返回值为int
    public static int getCurrentDay() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.DAY_OF_MONTH);
    }

    //获取当前号
    public static String getStrcurrentDay() {
        Calendar calendar = Calendar.getInstance();
        int CurrentDAY_OF_MONTH = calendar.get(Calendar.DAY_OF_MONTH);
        DecimalFormat Dformat = new DecimalFormat("00");
        return Dformat.format(CurrentDAY_OF_MONTH);
    }


    //结合日历picker选择器,显示中文的
    public static String getChineseFormatFromPicker(int dateYear, int dateMonth, int dateDay) {
        String time = "";
        DecimalFormat Dformat = new DecimalFormat("0000");
        time = Dformat.format(dateYear) + "年";
        Dformat = new DecimalFormat("00");
        time += Dformat.format(dateMonth) + "月";
        time += Dformat.format(dateDay) + "日";

        return time;
    }

    //结合日历picker选择器
    public static String getyyyyMMddFormatFromPicker(int dateYear, int dateMonth, int dateDay) {
        String time = "";
        DecimalFormat Dformat = new DecimalFormat("0000");
        time = Dformat.format(dateYear) + "-";
        Dformat = new DecimalFormat("00");
        time += Dformat.format(dateMonth) + "-";
        time += Dformat.format(dateDay);

        return time;
    }

    public static String getStrDecimalFormatDate(int dateYear, int dateMonth, int dateDay, int hours, int mimute, int second) {
        String actime = "";

        // 日期拼接，缺失补零
        DecimalFormat Dformat = new DecimalFormat("0000");
        actime += Dformat.format(dateYear) + "-";
        // 日期拼接，缺失补零
        Dformat = new DecimalFormat("00");
        actime += Dformat.format(dateMonth) + "-";
        actime += Dformat.format(dateDay) + " ";

        actime += Dformat.format(hours) + ":";

        actime += Dformat.format(mimute) + ":";

        actime += Dformat.format(second);
        return actime;
    }

    //获取当年月
    public static String getStrCurrentYearandMonth() {
        String CurrentYEAR = getStrCurrentYear();
        String CurrentMonth = getStrCurrentMonth();
        String currentYM = CurrentYEAR + "-" + CurrentMonth;
        return currentYM;
    }

    //获取当年月日
    public static String getStrCurrentYearandMonthandDay() {
        String CurrentYEAR = getStrCurrentYear();
        String CurrentMonth = getStrCurrentMonth();
        String CurrentDay = getStrcurrentDay();
        String currentYMD = CurrentYEAR + "-" + CurrentMonth + "-" + CurrentDay;
        return currentYMD;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param pastdaynumber
     * @return
     */
    public static String getCurrentTimePastDate(int pastdaynumber) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - pastdaynumber);
        Date date = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat(patternyMd);
        String result = format.format(date);
        HiLog.info(hiLogLabel, CurrentClassName + "获取过去第几天的日期为 " + result);
        return result;
    }

    //首页过去七天
    public static String getPastSevenDaysStartToEnd() {
        Calendar calendar = Calendar.getInstance();
        //算出七天前
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - 6);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("MM月dd日");
        String result = format.format(today);

        return result + "-" + getCurrentMonth() + "月" + getCurrentDay() + "日";
    }

    //本月统计，返回月开头
    public static String[] getListMonthStartToCurrentDays() {
        Calendar calendar = Calendar.getInstance();
        int index = calendar.get(Calendar.DAY_OF_MONTH);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String[] currentMonth = new String[index];
        for (int i = 0; i < index; i++) {
            currentMonth[i] = sdf.format(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH, -1);
        }
        return currentMonth;
    }


    //首页本月字符设置
    public static String getCurrentMonthStartToEnd() {
        LocalDate now = LocalDate.now();
        LocalDate firstday = LocalDate.of(now.getYear(), now.getMonth(), 1);
        LocalDate lastDay = now.with(TemporalAdjusters.lastDayOfMonth());
        return firstday.getMonthValue() + "月" + firstday.getDayOfMonth() + "日-" +
                lastDay.getMonthValue() + "月" + lastDay.getDayOfMonth() + "日";
    }

    //过去七天的饼状图，折线图和柱状图需要
    public static String[] getPastSevenDate() {
        String[] pastSevenDate = new String[7];
        for (int i = 0; i < 7; i++) {
            pastSevenDate[i] = getCurrentTimePastDate(i);
        }
        return pastSevenDate;
    }

    //得到字符串，把一个date对象转化为字符串
    public static String getStrTimeFromDateTime(Date time) {
        //格式
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);

        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        //格式的字符串
        String strTime = formatter.format(time);
        return strTime;
    }

    //得到字符串，把一个date对象转化为字符串
    public static Date getDateTimeFromStrTime(String strTime) {
        Date date = null;
        //格式
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);

        formatter.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));

        try {
            //格式的字符串
            date = formatter.parse(strTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String getChineseFormatFromStr(String time) {
        if ((time == null) || (time.equals(""))) {
            return null;
        }
        Date date = null;
        try {
            date = new Date(dateFormat.parse(time).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        DateFormat dateFormat = new SimpleDateFormat(patternyMdBWithChinese);
        return dateFormat.format(date);
    }



    public static Date parseTimeString2Date(String timeString) {
        if ((timeString == null) || (timeString.equals(""))) {
            return null;
        }
        Date date = null;
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        try {
            date = new Date(dateFormat.parse(timeString).getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    public static String convertDate2String(Date date, String pattern) {
        if (date == null)
            return null;
        DateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }


    public static int getYear(String timeString) {
        String timeStr = convertDate2String(parseTimeString2Date(timeString), "yyyy-MM-dd HH:mm:ss");
        return Integer.parseInt(timeStr.substring(0, 4));
    }

    public static int getMonth(String timeString) {
        String timeStr = convertDate2String(parseTimeString2Date(timeString), "yyyy-MM-dd HH:mm:ss");
        return Integer.parseInt(timeStr.substring(5, 7));
    }

    public static int getDay(String timeString) {
        String timeStr = convertDate2String(parseTimeString2Date(timeString), "yyyy-MM-dd HH:mm:ss");
        return Integer.parseInt(timeStr.substring(8, 10));
    }
    //******************************************
    public static int getHour(String timeString) {
        String timeStr = convertDate2String(parseTimeString2Date(timeString), "yyyy-MM-dd HH:mm:ss");
        return Integer.parseInt(timeStr.substring(11, 13));
    }

    public static int getMinute(String timeString) {
        String timeStr = convertDate2String(parseTimeString2Date(timeString), "yyyy-MM-dd HH:mm:ss");
        return Integer.parseInt(timeStr.substring(14, 16));
    }

    public static int getSecond(String timeString) {
        String timeStr = convertDate2String(parseTimeString2Date(timeString), "yyyy-MM-dd HH:mm:ss");
        return Integer.parseInt(timeStr.substring(17, 19));
    }
    //******************
}
